package com.skyline.jfs.common.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;

/**
 * 
 * ͨ����࣬�κ��������п��Ծ�̬��ȡSpring�е�������Ϣ
 * 
 * @author wuqh
 */
public class SpringUtils implements ApplicationContextAware {
	private static ApplicationContext applicationContext;

	/**
	 * ʵ��ApplicationContextAware�ӿ�,ע��ApplicationContext
	 * 
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringUtils.applicationContext = applicationContext;
	}

	/**
	 * ��ȡApplicationContext
	 * 
	 * @return
	 */
	public static ApplicationContext getApplicationContext() {
		checkApplicationContext();
		return applicationContext;
	}
	
	/**
	 * ��ȡSpring�е�bean���μ�{@link org.springframework.beans.factory.BeanFactory#getBean(String)}
	 * 
	 * @param name bean��name��id
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		checkApplicationContext();
		return (T) applicationContext.getBean(name);
	}

	public static <T> T getBean(Class<T> requiredType) {
		return applicationContext.getBean(requiredType);
	}
	
	/**
	 * ���applicationContext
	 */
	public static void cleanApplicationContext() {
		applicationContext = null;
	}

	private static void checkApplicationContext() {
		Assert.notNull(applicationContext, "Springδ�ܳɹ�ע��");
	}
}
